package com.sunwoda.springbootredis.service.impl;

import com.sunwoda.springbootredis.dao.RequestOrderLineDao;
import com.sunwoda.springbootredis.dto.RequestOrderDto;
import com.sunwoda.springbootredis.dto.RequestOrderLineDto;
import com.sunwoda.springbootredis.entity.RequestOrder;
import com.sunwoda.springbootredis.dao.RequestOrderDao;
import com.sunwoda.springbootredis.entity.RequestOrderLine;
import com.sunwoda.springbootredis.service.RequestOrderService;
import com.sunwoda.springbootredis.util.ExcelUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

/**
 * (RequestOrder)表服务实现类
 *
 * @author makejava
 * @since 2021-09-23 14:22:04
 */
@Service("requestOrderService")
@CacheConfig(cacheNames = "RequestOrder")
public class RequestOrderServiceImpl implements RequestOrderService {
    @Resource
    private RequestOrderDao requestOrderDao;

    @Resource
    private RequestOrderLineDao requestOrderLineDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public RequestOrder queryById(Integer id) {
        return this.requestOrderDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<RequestOrder> queryAllByLimit(int offset, int limit) {
        return this.requestOrderDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param requestOrder 实例对象
     * @return 实例对象
     */
    @Override
    public RequestOrder insert(RequestOrder requestOrder) {
        this.requestOrderDao.insert(requestOrder);
        return requestOrder;
    }

    /**
     * 修改数据
     *
     * @param requestOrder 实例对象
     * @return 实例对象
     */
    @Override
    public RequestOrder update(RequestOrder requestOrder) {
        this.requestOrderDao.update(requestOrder);
        return this.queryById(requestOrder.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.requestOrderDao.deleteById(id) > 0;
    }

    @Override
    public void export(RequestOrder rt, HttpServletResponse response) {
        List<RequestOrder> orderList = this.requestOrderDao.queryAll(rt);
        ArrayList<RequestOrderDto> list = new ArrayList<>();
        orderList.forEach(one -> {
            RequestOrderDto dto = new RequestOrderDto();
            RequestOrderLine line = new RequestOrderLine();
            line.setRequestOrderId(one.getId());
            List<RequestOrderLine> orderLines = this.requestOrderLineDao.queryAll(line);
            ArrayList<RequestOrderLineDto> lineDtos = new ArrayList<>();
            orderLines.forEach(lineDto -> {
                RequestOrderLineDto requestOrderLineDto = new RequestOrderLineDto();
                BeanUtils.copyProperties(lineDto,requestOrderLineDto);
                lineDtos.add(requestOrderLineDto);
            });
            BeanUtils.copyProperties(one,dto);
            dto.setLineDtoList(lineDtos);
            list.add(dto);
        });
        ExcelUtil.exportExcel(list,"订单列表","订单列表",RequestOrderDto.class,"订单列表.xls",response);
    }

    @Override
    public void OrderImport(MultipartFile file) {
        List<RequestOrderDto> orderDtos = ExcelUtil.importExcel(file, 1, 1, RequestOrderDto.class);
        ArrayList<RequestOrder> head = new ArrayList<>();
        List<RequestOrderLine> orderLines = new ArrayList<>();
        for (RequestOrderDto dto : orderDtos) {
            RequestOrder order = new RequestOrder();
            BeanUtils.copyProperties(dto,order);
            head.add(order);

            dto.getLineDtoList().forEach(line -> {
                RequestOrderLine orderLine = new RequestOrderLine();
                BeanUtils.copyProperties(line,orderLine);
                orderLines.add(orderLine);
            });
        }
        int batch = this.requestOrderDao.insertBatch(head);
        int i = this.requestOrderLineDao.insertBatch(orderLines);
    }

    @Override
    @Cacheable(key = "#re.id")
    public List<RequestOrder> getList(RequestOrder re) {
        List<RequestOrder> list = this.requestOrderDao.queryAll(re);
        return list;
    }

    @Override
    @CachePut(key = "#re.id")
    public int updateOrder(RequestOrder re) {
        int update = this.requestOrderDao.update(re);
        return update;
    }
}
